package com.andy.wan.android.mvp.ui.fragment;

import android.view.View;

import com.andy.ui.base.BaseListFragment;
import com.andy.ui.base.SimpleWebActivity;
import com.andy.ui.widget.PageStateView;
import com.andy.wan.android.R;
import com.andy.wan.android.mvp.api.WanAndroidApi;
import com.andy.wan.android.mvp.bean.BaseResponse;
import com.andy.wan.android.mvp.bean.FeedArticleData;
import com.andy.wan.android.mvp.bean.FeedArticleListData;
import com.andy.wan.android.mvp.bean.ProjectClassifyData;
import com.andy.wan.android.mvp.net.RetrofitUtil;
import com.andy.wan.android.mvp.ui.adapter.ProjectAdapter;
import com.andy.wan.android.mvp.ui.adapter.ProjectFragmentPagerAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import io.reactivex.Observable;

/**
 * @Author: luofan
 * @CreateDate: 2020/3/28 16:33
 * @Description:
 */
public class ProjectListFragment extends BaseListFragment<BaseResponse<FeedArticleListData>, FeedArticleData> {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout mSmartRefresh;
    @BindView(R.id.page_stat_view)
    PageStateView mPageStatView;

    private ProjectClassifyData mProjectClassifyData;

    @Override
    public void initData() {
        mProjectClassifyData = getArguments().getParcelable(ProjectFragmentPagerAdapter.DATA);
    }

    @NonNull
    @Override
    protected Observable<BaseResponse<FeedArticleListData>> getObservable() {
        return RetrofitUtil.getInstance().create(WanAndroidApi.class).getProjectListData(getPageNum(), mProjectClassifyData.getId());
    }

    @Override
    protected List<FeedArticleData> getRecyclerDataFromResult(BaseResponse<FeedArticleListData> response) {
        return response.getData().getDatas();
    }

    @NonNull
    @Override
    protected RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    @Nullable
    @Override
    protected SmartRefreshLayout getRefreshLayout() {
        return mSmartRefresh;
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<FeedArticleData, BaseViewHolder> getAdapter() {
        ProjectAdapter projectAdapter = new ProjectAdapter();
        projectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                FeedArticleData bean = projectAdapter.getItem(position);
                if (bean != null) {
                    SimpleWebActivity.startMe(getContext(), bean.getLink(), bean.getTitle());
                }
            }
        });
        return projectAdapter;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_project_list;
    }

    @Override
    protected PageStateView getPageStateView() {
        return mPageStatView;
    }
}
